import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:visitor_scan_package/net/account/axun_user.dart';
import 'package:visitor_scan_package/net/api.dart';
import 'package:visitor_scan_package/ui/home_page.dart';

class LoginViewModel extends BaseViewModel {
  var username = "";
  var pdd = "";

  LoginViewModel(BuildContext context) : super(context);

  Future<void> login() async {
    if (username.isNullOrEmpty()) {
      showToast("请输入账号");
      return;
    }

    if (pdd.isNullOrEmpty()) {
      showToast("请输入密码");
      return;
    }

    showLoadingDialog(msg: '登录中...');
    try {
      var data = await PostRequest.login(username, pdd);
      hideDialog();
      if (data['code'] == "0") {
        AxunUser.saveInfo(username, data['id'].toString(),
            data['userCaption'].toString(), pdd, 'accountId');
        AxunUser.init();
        Go().pushAndPop(ScanVisitorHomePage());
      } else {
        showToast(data['msg']);
      }
    } catch (e) {
      showToast(e.toNetError());
      hideDialog();
    }
  }
}
